package com.gugee.tiktok.data.consumer.blogger.core.main;

import com.gugee.common.thread.ScheduleTemplate;
import com.gugee.tiktok.common.model.dal.blogger.AwemeRepository;
import com.gugee.tiktok.common.model.spider.aweme.Aweme_list;
import com.gugee.tiktok.common.model.spider.aweme.BloggerAwemesResponse;
import com.gugee.tiktok.data.consumer.blogger.config.DataConsumerConfig;
import com.gugee.tiktok.data.consumer.blogger.core.service.CollectService;
import com.gugee.tiktok.data.consumer.blogger.core.spider.AwemeSpiderClient;
import com.xiguaji.boot.eventbus.EventBusTemplate;
import com.xiguaji.boot.toolkit.DateTimeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.PostConstruct;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: luolifeng
 * Date: 2020-10-14 18:29
 */
@Slf4j
@Component
public class SaveBloggerAwemeMain {

    @Autowired
    @Lazy
    EventBusTemplate eventBusTemplate;

    @Autowired
    AwemeSpiderClient awemeSpiderClient;

    @Autowired
    SaveAwemeEventMain saveAwemeEventMain;

    @Autowired
    CollectService collectService;
    @Autowired
    AwemeRepository awemeRepository;

    @Autowired
    DataConsumerConfig dataConsumerConfig;

    private ThreadPoolExecutor customPoolMain;
    @Autowired
    ScheduleTemplate scheduleTemplate;
    final String updateBloggerMain = "UpdateBlogger-";
    @PostConstruct
    public void init() {
        log.info("UpdateBloggerMainEventListener  init");
        customPoolMain = scheduleTemplate.getCustomThreadPool(updateBloggerMain, dataConsumerConfig.getCorePollSize(), dataConsumerConfig.getCorePollSize(), 0, dataConsumerConfig.getQueueSize(), TimeUnit.MINUTES);
    }
    public void handle(String bloggerType,List<Long> uids) {
        long start = System.currentTimeMillis();
         uids.forEach(uid -> {
            try {
                handle(bloggerType,uid);
            } catch (Exception ex) {
                ex.printStackTrace();
                log.error("SaveBloggerAweme error. uid:" + uid+ex.getMessage());
            }
        });
    }

    public void handle(String bloggerType,long uid) {
        long max_cursor = 0;
        int count = 40;
        boolean isHasMore = true;
        //预先分配一定空间，防止频繁自动扩容带来的深拷贝的内存消耗
        long condition = DateTimeUtils.second(LocalDateTime.now().minusDays(dataConsumerConfig.getLatestDays()+2));
        int cnt = awemeRepository.countAwemesByOUidAndCreateTime(uid, condition);
        cnt = cnt * 4 / 3 + 1;
        List<Aweme_list>  awemes = new ArrayList<>(cnt);

        long totalStart = System.currentTimeMillis();
//        try {
            while (isHasMore) {
                try {
                    BloggerAwemesResponse response = awemeSpiderClient.getBloggerAwemes(uid, max_cursor, count);
                    if (response == null || CollectionUtils.isEmpty(response.getAweme_list())) break;
                    isHasMore = response.isHas_more();
                    max_cursor = response.getMax_cursor();

                    //如果属于最近90天的列表则收录
                    awemes.addAll(response.getAweme_list().stream()
                            .filter(a -> a.getCreate_time() > condition)
                            .collect(Collectors.toList()));
                    //awemeSpiderClient.getBloggerAwemes，目前response.getMax_cursor()最多返回33个，count大于33不起效果
                    if (response.getMax_cursor() <= condition * 1000) {
                        break;
                    }
                }catch(Exception e){
                    log.error("uid={},msg={}",uid,e.getMessage());
                    break;
                }

            }
            if((System.currentTimeMillis()-totalStart)/1000>dataConsumerConfig.getDebugTimeLimit()) {
                log.info(" blogger: " + uid + " ,awemes.size="+awemes.size()+", 爬取视频花费:" + (System.currentTimeMillis() - totalStart) / 1000 + "s");
            }

//        } catch (Exception ex) {
//            //记录异常，继续进行接下去的操作
//            log.error(" uid:" + uid + " count:" + awemes.size()+ ":"+ex.getMessage());
//
//        }

        if (awemes.size() > 0) {
            saveAwemeEventMain.handle(bloggerType,uid, awemes);
            //放入线程池中
            try {
                customPoolMain.submit(() ->  {
//                    todo 音乐收录，可以异步处理或发送至消息队列
//                    collectService.collectMusic(awemes);
//                    todo 话题收录，可以异步处理或发送至消息队列
//                    collectService.collectHashTag(awemes);
//                    todo 话题与提及收录，可以异步处理或发送至消息队列

                    collectService.collectAwemeMap2HashTag(awemes,uid);
                    collectService.collectAwemeMap2Mention(awemes,uid);
                });
            } catch (Exception ex) {
                log.error("collectService thread error.  {}" ,  ex.getMessage());
            }
        }
    }
}
